package Classes.Bullet 
{
	import flash.utils.Dictionary;
	import net.flashpunk.World;
	import net.flashpunk.Entity;
	import flash.geom.Point
	import net.flashpunk.graphics.Image;
	import net.flashpunk.FP;
	import Classes.Collisions.*;
	/**
	 * ...
	 * @author Alex
	 */
	public class Bullet extends Entity
	{
		private var m_speed:uint;
		private var m_type:String;
		private var m_status:String;
		private var m_pic:Image;
		private var m_directionFired:String;
		private var m_velocity:Point;
		
		public function Bullet(pointFired:Point,directionFired:String) 
		{
			type = ObjectTypes.BULLET;
			var image:Image = new Image(TankImageList.GFX_BASIC_BULLET);
			setImage(image);
			setHitbox(image.width, image.height, x, y);
			pointFired = getOffset(pointFired, directionFired);
			setAngle(directionFired);
			this.x = pointFired.x;
			this.y = pointFired.y;
			this.m_speed = 10;
			this.m_directionFired = directionFired;
			this.m_velocity = new Point();
			MoveBullet();
		}
		
		private function getOffset(pointFired:Point,directionFired:String):Point
		{
			var distanceFromPic:int = 2;
			var offset:int = (m_pic.width / 2) + distanceFromPic; 
			
			if (directionFired == Direction.UP)
			{
				pointFired.y = pointFired.y-offset;
			}
			
			if (directionFired == Direction.DOWN)
			{
				pointFired.y = pointFired.y+offset;
			}
			
			if (directionFired == Direction.LEFT)
			{
				pointFired.x=pointFired.x-offset;
			}
			
			if (directionFired == Direction.RIGHT)
			{
				pointFired.x=pointFired.x+offset;	
			}
			
			return pointFired;
		}
		
		private function setAngle(unconvertedDirectionFired:String):void
		{
			var conversionDict:DictionaryOfAngles = new DictionaryOfAngles();
			var dict:Dictionary = conversionDict.getDictOfAngles();	
			var newAngle:int = dict[unconvertedDirectionFired];
			m_pic.centerOrigin();
			m_pic.angle = newAngle;
			setImage(m_pic);
			setHitbox(m_pic.width, m_pic.height, x, y);
		}
		
		public function setImage(image:Image):void
		{
			m_pic = image;
			graphic = m_pic;
		}
		
		private function MoveBullet():void
		{
			var directionFired:String = m_directionFired;
			var trajectory:Point = new Point();
			trajectory.x = 0;
			trajectory.y = 0;
			
			//movementDirection will be 0,1, or -1
			var movementDirection:Point = new Point;
			
			if (directionFired == Direction.UP)
			{
				movementDirection.y--;
			}
			
			if (directionFired == Direction.DOWN)
			{
				movementDirection.y++;
			}
			
			if (directionFired == Direction.LEFT)
			{
				movementDirection.x--;
			}
			
			if (directionFired == Direction.RIGHT)
			{
				movementDirection.x++;	
			}
			this.m_velocity.x = movementDirection.x;
			this.m_velocity.y = movementDirection.y;
			this.x += m_speed*movementDirection.x;
			this.y += m_speed*movementDirection.y;
		}
		
		private function getCollisions():Array
		{	
			var collisionDetector:CollisionDetector = new CollisionDetector();
			var typesCollidedWith:Array = collisionDetector.getCollisions(this);
			
			return typesCollidedWith;
			
		}
		
		private function collisionHandling():void
		{
			var typesCollidedWith:Array = getCollisions();
			for each (var typeCollidedWith:Entity in typesCollidedWith)
			{
				if (typeCollidedWith.type == ObjectTypes.LEVEL)
					{
						FP.world.remove(this);
					}
				if (typeCollidedWith.type == ObjectTypes.ENEMY_TANK)
					{
						FP.world.remove(this);	
					}
			}
			
		}
		
	
		
		
		private function setStatus(status:String):void
		{
			this.m_status = status;
			
		}
		
		private function getStatus():String
		{
			return this.m_status;
			
		}
		
		override public function update():void 
		{
			collisionHandling();
			MoveBullet();
			super.update();
		}
	}
	
}